<template>
  <div class="result-box">
    <van-nav-bar
      left-arrow
      @click-left="$router.back()"
      title="搜索中心"
    >
    </van-nav-bar>
    <van-list
      class="article-list"
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell
        v-for="item in list"
        :key="item.art_id.toString()"
        :title="item.title"
        @click="$router.push('/article/'+item.art_id.toString())"
      />
    </van-list>
  </div>
</template>
<script>
import { getSearchRes } from '../../api/search.js'
export default {
  name: 'Result',
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 1
    }
  },
  methods: {
    async onLoad () {
      // 异步更新数据
      // setTimeout 仅做示例，真实场景中一般为 ajax 请求
      const res = await getSearchRes(this.$route.query.keyword, this.page)
      // console.log('searchRes', res)
      this.list.push(...res.data.data.results)

      // 加载状态结束
      this.loading = false

      // 数据全部加载完成
      if (res.data.data.results.length === 0) {
        this.finished = true
      }
      this.page++
    }
  }
}
</script>
<style lang="less" scoped>
.result-box {
  height: 100%;
  overflow: auto;
  .article-list {
    margin-top: 39px;
  }
}
</style>
